﻿@model NewsletterBoxModel
@using Nop.Web.Models.Newsletter;
<div class="block block-newsletter">
    <div class="title">
        <strong>@T("Newsletter.Title")</strong>
    </div>
    <div class="listbox">
        <div id="newsletter-subscribe-block" class="newsletter-subscribe-block">
            <span>@T("Newsletter.Subscribe"):</span>
            <div class="newsletter-email">
                @Html.TextBoxFor(x => x.NewsletterEmail, new { id = "newsletter-email", @class = "form-control" })
                @Html.ValidationMessageFor(x => x.NewsletterEmail)
            </div>
            <div class="buttons">
                <button type="button" id="newsletter-subscribe-button" class="btn btn-info btn-xs newsletter-subscribe-button"> @T("Newsletter.Button")</button>
                <span id="subscribe-loading-progress" style="display: none;" class="please-wait">@T("Common.Wait...")</span>
                <span id="newsletter-result-block" class="text-danger"></span>
            </div>
        </div> 
    </div>
    <script type="text/javascript">
        seajs.use("jquery", function ($) {
            $(function () {
                $('#newsletter-subscribe-button').click(function () {
                    var email = $("#newsletter-email").val();
                    var subscribeProgress = $("#subscribe-loading-progress");
                    subscribeProgress.show();
                    $.ajax({
                        cache: false,
                        type: "POST",
                        url: "@(Url.RouteUrl("SubscribeNewsletter"))",
                        data: { "email": email },
                        success: function (data) {
                            subscribeProgress.hide();
                            $("#newsletter-result-block").html(data.Result);
                            if (data.Success) {
                                $('#newsletter-subscribe-block').hide();
                                $('#newsletter-result-block').show();
                            }
                            else {
                                $('#newsletter-result-block').fadeIn("slow").delay(2000).fadeOut("slow");
                            }
                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            alert('Failed to subscribe.');
                            subscribeProgress.hide();
                        }
                    });
                    return false;
                });
            });
        });

    </script>
</div>
